package com.example.dangerouscargodriver.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.entry.LineInfo;

import java.util.ArrayList;
import java.util.List;

public class LineInfoAdapter extends RecyclerView.Adapter<LineInfoAdapter.ViewHolder>{

	private static final String TAG = "CompanyInfoAdapter";
	private List<LineInfo> mLineInfoItemList = null;
	private Context mContext;
	private ClickListener mClickListener;
	private View mRootView;
	private IUserActionListener mListener = null;

	public interface IUserActionListener{

		void onEdit(int position);

		void onDelete(int position);
	}

	public void setUserActionListener(IUserActionListener listener){
		mListener = listener;
	}

	static class ViewHolder extends RecyclerView.ViewHolder {
		private TextView mStartPoint = null;
		private TextView mEndPoint = null;
		private TextView mEdit = null;
		private TextView mDelete = null;

		public ViewHolder(View view) {
			super(view);
			mStartPoint = view.findViewById(R.id.tv_text_start_point);
			mEndPoint = view.findViewById(R.id.tv_text_end_point);
			mEdit = view.findViewById(R.id.tv_action_edit);
			mDelete = view.findViewById(R.id.tv_action_delete);
		}
	}

	public LineInfoAdapter() {
		mLineInfoItemList = new ArrayList<>();
		mClickListener = new ClickListener();
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		mRootView = LayoutInflater.from(parent.getContext()).inflate(
				R.layout.layout_line_info_item, parent, false);
		ViewHolder holder = new ViewHolder(mRootView);

		return holder;
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		LineInfo lineInfo = mLineInfoItemList.get(position);

		holder.mStartPoint.setText(lineInfo.getFrom_addr());
		holder.mEndPoint.setText(lineInfo.getTo_addr());
		holder.mEdit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.onEdit(position);
				}
			}
		});

		holder.mDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mListener != null){
					mListener.onDelete(position);
				}
			}
		});

		holder.itemView.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				mClickListener.onClick(position);
				notifyDataSetChanged();
			}
		});
	}

	@Override
	public int getItemCount() {
		return mLineInfoItemList.size();
	}

	public class ClickListener {
		private int mPosition = -1;

		void onClick(int position){
			mPosition = position;
		}

		public int getPosition() {

			return mPosition;
		}

		public void setPosition(int mPosition) {

			this.mPosition = mPosition;
		}
	}

	public void addData(int position, LineInfo item) {
		mLineInfoItemList.add(position, item);
		notifyItemInserted(position);
		notifyDataSetChanged();
	}

	public void removeData(int position) {
		mLineInfoItemList.remove(position);
		notifyItemRemoved(position);
		notifyDataSetChanged();
	}

	public void updateData(int position, LineInfo item) {
		mLineInfoItemList.set(position, item);
		notifyItemChanged(position);
	}

	public void clearData(){
		mLineInfoItemList.clear();
		notifyDataSetChanged();
	}

	public void appendData(List<LineInfo> array) {
		for(int index = 0; index < array.size(); index++){
			mLineInfoItemList.add(array.get(index));
		}
		notifyDataSetChanged();
	}
}
